package yyy.ab.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import yyy.ab.common.core.BaseController;
import yyy.ab.common.core.ResultBean;
import yyy.ab.modules.system.entity.SysRole;
import yyy.ab.modules.system.entity.dto.SysRoleMenuAllocationDto;
import yyy.ab.modules.system.entity.dto.SysRoleMenuDto;
import yyy.ab.modules.system.entity.dto.SysRoleSmallDto;
import yyy.ab.modules.system.service.SysRoleService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Api(tags = "系统：角色管理")
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    private final SysRoleService roleService;

    /**
     * 角色页面
     */
    @RequestMapping
    public ModelAndView jump() {
        ModelAndView view = new ModelAndView();
        view.setViewName("system/role/index");
        return view;
    }

    //    @Log("查询角色")
    @ApiOperation("查询角色")
    @GetMapping("/findPage")
    public Object query(@RequestParam Map<String, Object> params) {
        IPage<SysRole> page = roleService.findPage(params);
        return jsonForTable(page);
    }

    @ApiOperation("查询前端角色列表")
    @GetMapping("/findSmallList")
    public Object findSmallList() {
        List<SysRoleSmallDto> smallList = roleService.findSmallList();
        return smallList;
    }

    @ApiOperation("创建角色")
    @PostMapping
    public Object create(@RequestBody SysRole role) {
        ResultBean resultBean = roleService.create(role);
        return resultBean;
    }

    @ApiOperation("修改角色")
    @PutMapping
    public Object update(@RequestBody SysRole role) {
        ResultBean resultBean = roleService.update(role);
        return resultBean;
    }

    @ApiOperation("删除角色")
    @DeleteMapping
    public Object deleteAll(@RequestBody Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        ResultBean resultBean = roleService.deleteAll(idList);
        return resultBean;
    }

    @ApiOperation("获取角色分配的菜单集合")
    @GetMapping("/getRoleMenuList")
    public Object getRoleMenuList(Long roleId) {
        List<SysRoleMenuDto> roleMenuList = roleService.getRoleMenuList(roleId);
        return roleMenuList;
    }

    @ApiOperation("分配角色分配的菜单")
    @PostMapping("/allocation")
    public Object allocation(@RequestBody SysRoleMenuAllocationDto roleMenuAllocationDto) {
        ResultBean resultBean = roleService.allocation(roleMenuAllocationDto);
        return resultBean;
    }
}
